/**
 * 用于占用进程
 *
 * @export
 * @param {*} duration 占用时长，单位 ms
 * @returns
 */
export function sleep(duration) {
  return new Promise(resolve => {
    setTimeout(resolve, duration);
  });
}

export function watermark(url) {
  const water =
    "?x-oss-process=image/resize,w_500/quality,q_100/watermark,image_ZXhhbXBsZS5wbmc=,g_ne/watermark,image_ZXhhbXBsZS5wbmc=,g_center/watermark,image_ZXhhbXBsZS5wbmc=,g_sw";

  if (url.indexOf("aliyuncs") > -1) {
    url = url.split("?")[0] + water;
  }

  return url;
}

export function getQueryString(name) {
  const res = location.href.match(new RegExp("[?&]" + name + "=([^&#]+)", "i"));

  if (res == null || res.length < 1) {
    return null;
  }

  return decodeURIComponent(res[1]);
}

export function formatNumber(number) {
  number = number || 0;
  number = Number(number);

  return (number / 10000).toFixed(1);
}

export function getDomain() {
  if (process.env.ENV === "dev") {
    return `${location.protocol}//${location.host}/#`;
  } else {
    return `${location.protocol}//${location.host}`;
  }
}
